package com.wy.cached;

import com.alibaba.fastjson.JSON;
import com.wy.constant.RedisConst;
import com.wy.domain.AreaCity;
import com.wy.domain.AreaCounty;
import com.wy.domain.AreaProvince;
import com.wy.service.IAreaCityService;
import com.wy.service.IAreaCountyService;
import com.wy.service.IAreaProvinceService;
import com.wy.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AreaCached {

	@Autowired
	private IAreaProvinceService areaProvinceService;

	@Autowired
	private IAreaCityService areaCityService;

	@Autowired
	private IAreaCountyService areaCountyService;
	
	public void initArea() throws Exception{
		List<AreaProvince> lists = areaProvinceService.findAll();
		if(null!=lists&&lists.size()>0){
//			CompanyCategoryVOUtil companyCategoryVOUtil = new CompanyCategoryVOUtil();
//			ICompanyCategoryService companyCategoryServiceImpl = (ICompanyCategoryService)CustomBeanFactory.getContext(CustomBeanConst.ADMIN_SERVICE_PATHS).getBean("companyCategoryServiceImpl");
			List<AreaCity> citys = null;
			List<AreaCounty> countys = null;;
//			List<CompanyCategoryCountVO> companyCategoryCountVOs = null;
			
			for(AreaProvince province:lists){
				citys = areaCityService.findList("provinceId",province.getId());
				if(null!=citys&&citys.size()>0){
					for(AreaCity city :citys){
						countys = areaCountyService.findList("cityId", city.getId());
						if(null!=countys&&countys.size()>0){
//							for(AreaCounty areaCounty:countys){
//								companyCategoryCountVOs = companyCategoryServiceImpl.findCompanyCategoryCountVOListByAreaCountyId(areaCounty.getId());
//								companyCategoryCountVOs = companyCategoryVOUtil.getCompanyCategoryCountVOListByAreaCountyId(areaCounty.getId());
//								if(null!=companyCategoryCountVOs&&companyCategoryCountVOs.size()>0){
//								RedisUtil.getInstance().del(RedisConst.AREA_COUNTY_CATEGORY_COUNT+areaCounty.getId());
//								RedisUtil.getInstance().set(RedisConst.AREA_COUNTY_CATEGORY_COUNT+areaCounty.getId(), JSONArray.fromObject(companyCategoryCountVOs).toString(),TimeConst.TWO_DAY);
//								}
//							}
							RedisUtil.getInstance().del(RedisConst.AREA_COUNTY+city.getId());
							RedisUtil.getInstance().set(RedisConst.AREA_COUNTY+city.getId(), JSON.toJSONString(countys));
							countys.clear();
							countys = null;
						}
						RedisUtil.getInstance().del(RedisConst.AREA_CITY_INFO_ID+city.getId());
						RedisUtil.getInstance().set(RedisConst.AREA_CITY_INFO_ID+city.getId(), JSON.toJSONString(city));
					}
					RedisUtil.getInstance().del(RedisConst.AREA_CITY+province.getId());
					RedisUtil.getInstance().set(RedisConst.AREA_CITY+province.getId(), JSON.toJSONString(citys));
					citys.clear();
					citys=null;
				}
				RedisUtil.getInstance().del(RedisConst.AREA_PROVINCE_INFO_ID+province.getId());
				RedisUtil.getInstance().set(RedisConst.AREA_PROVINCE_INFO_ID+province.getId(),JSON.toJSONString(province));
			}
			RedisUtil.getInstance().del(RedisConst.AREA_PROVINCE);
			RedisUtil.getInstance().set(RedisConst.AREA_PROVINCE, JSON.toJSONString(lists));
			lists.clear();
		}
	}
	
	public static void main(String[] args) {
//		System.out.println(JedisUtil.getInstance().get(JedisUtil.getInstance());
	}
	
}